let express = require ('express');
let path = require ('path');
let favicon = require ('serve-favicon');
let logger = require ('morgan');
let cookieParser = require ('cookie-parser');
let bodyParser = require ('body-parser');
let accessTokenController = require('./src/node/controllers/AccessTokenController');
accessTokenController.initAccessToken();
let index = require ('./src/node/routes/index');
let weiXin = require('./src/node/routes/weixin');
let cinema = require('./src/node/routes/cinema');
let app = express ();
/*const webpack = require ('webpack');
const webpackDevMiddleware = require ('webpack-dev-middleware');
const webpackHotMiddleware = require ('webpack-hot-middleware');
const webpackDevConfig = require ('./webpack.config.js');
const compiler = webpack (webpackDevConfig);
app.use (webpackDevMiddleware (compiler, {
    // public path should be the same with webpack config
    publicPath: webpackDevConfig.output.publicPath,
    noInfo    : true,
    stats     : {
        colors: true,
        chunks: false
    }
}));
app.use (webpackHotMiddleware (compiler));*/
// view engine setup
app.set ('views', path.join (__dirname, 'src/web/views'));
app.set ('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use (logger ('dev'));
app.use (bodyParser.json ());
app.use (bodyParser.urlencoded ({extended: false}));
app.use (cookieParser ());
app.use (express.static (path.join (__dirname, 'src/web/public')));

app.use ('/', index);
app.use('/weixin',weiXin);
app.use('/cinema',cinema);
// catch 404 and forward to error handler
app.use (function ( req, res, next )
{
    let err = new Error ('Not Found');
    err.status = 404;
    next (err);
});

// error handler
app.use (function ( err, req, res, next )
{
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get ('env') === 'development' ? err : {};
    
    // render the error page
    res.status (err.status || 500);
    res.render ('error');
});

module.exports = app;
